package com.zxl.admin.controller.order;

import cn.hutool.core.stream.StreamUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.zxl.admin.annotation.NoPower;
import com.zxl.admin.service.OrderService;
import com.zxl.common.Page;
import com.zxl.common.Result;
import com.zxl.common.exception.BusinessException;
import com.zxl.common.model.bo.order.OrderStatusBo;
import com.zxl.common.model.pojo.SysOrderLogistics;
import com.zxl.common.model.query.order.OrderPageQuery;
import com.zxl.common.model.query.order.RefundQuery;
import com.zxl.common.model.vo.order.OrderDetailsVo;
import com.zxl.common.model.vo.order.OrderPageVo;
import com.zxl.common.service.SysOrderLogisticsService;
import com.zxl.common.utils.HttpHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * @author zxw
 * @date 2023/4/1
 */
@Api(tags = "订单管理")
@RestController
@RequestMapping("/admin/order")
@Slf4j
public class OrderController {

    @Resource
    private OrderService orderService;
    @Resource
    private SysOrderLogisticsService orderLogisticsService;

    @GetMapping("/page")
    @ApiOperation("订单分页列表")
    public Result<Page<OrderPageVo>> getOrderPage(OrderPageQuery orderPageQuery) {
        List<OrderPageVo> orderPageVos = orderService.getOrderPage(orderPageQuery);
        Page<OrderPageVo> page = Page.pageInfoConvert(new PageInfo(orderPageVos));
        return new Result<>(page);
    }

    @GetMapping("/info")
    @ApiOperation("订单详情")
    public Result<OrderDetailsVo> getOrderDetail(@RequestParam @ApiParam(value = "订单编号") String orderCode) {
        OrderDetailsVo orderDetail = orderService.getOrderDetail(orderCode);
        return new Result<>(orderDetail);
    }


    @PostMapping("/update")
    @ApiOperation("修改订单状态")
    public Result updateOrderStatus(OrderStatusBo orderStatusBo) {
        orderService.updateOrderStatus(orderStatusBo);
        return Result.success();
    }

    @PostMapping("/send")
    @ApiOperation("/订单发货")
    public Result sendOrder(@Valid @RequestBody SysOrderLogistics sysOrderLogistics) {
        orderService.sendOrder(sysOrderLogistics);
        return Result.success();
    }

    @PostMapping("/refund")
    @ApiOperation("/退款")
    public Result refund(RefundQuery query) {
        if (orderService.refund(query)){
            return Result.success("退款成功");
        }
        return Result.fail("退款失败");
    }

    @ApiIgnore
    @ApiOperation("微信退款回调")
    @RequestMapping("/refundNotice")
    @NoPower
    public void refundNotice(HttpServletRequest request, HttpServletResponse response) {
        try {
            String reqParams = HttpHelper.getBodyString(request);
            log.info("-------退款回调结果:" + reqParams);
            JSONObject json = JSONObject.parseObject(reqParams);
            String event_type = json.getString("event_type");
            if (event_type.equals("REFUND.SUCCESS")) {
                log.info("-------退款回调成功");
                orderService.refundNotice(json);
            } else {
                log.info("-------退款回调失败");
            }

        } catch (Exception e) {
            throw new BusinessException("退款回调异常" + e.toString());
        }
    }


}
